//
//  UIImage+XFManager.m
//  WorkManager
//
//  Created by ukongm on 17/2/18.
//  Copyright © 2017年 ukongm. All rights reserved.
//

#import "UIImage+XFManager.h"

@implementation UIImage (XFManager)

- (UIImage *)imageScaledToSize:(CGSize)newSize{
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [self drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
}

- (UIImage *)imageToFrame:(CGRect)frame
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, frame);
    
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGContextDrawImage(context, smallBounds, subImageRef);
    
    UIImage* newImage = [UIImage imageWithCGImage:subImageRef];
    
    UIGraphicsEndImageContext();
    
    return newImage;
    
}

@end
